package cn.edu.zjgsu.ito.service.impl;

import java.util.Calendar;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.edu.zjgsu.ito.dao.VouchersMapper;
import cn.edu.zjgsu.ito.model.Vouchers;
import cn.edu.zjgsu.ito.model.VouchersExample;
import cn.edu.zjgsu.ito.model.VouchersExample.Criteria;
import cn.edu.zjgsu.ito.service.IVoucherService;
import cn.edu.zjgsu.ito.vo.Page;

@Service
public class VoucherServiceImpl implements IVoucherService
{
	@Autowired
	private VouchersMapper vouchersMapper;

	@Override
	public Page<Vouchers> getVouchers(int sequence, String key, int pageCount,
			int pageSize)
	{
		VouchersExample vouchersExample = new VouchersExample();
		Criteria criteria = vouchersExample.or();
		vouchersExample.setStart(Math.min(0, (pageCount-1)*pageSize));
		vouchersExample.setPageSize(pageSize);
		if(key!=null&&key.length()>=0)
		{
			criteria.andVouchersNameLike("%"+key+"%");
		}
		switch (sequence)
		{
		case 1:
			vouchersExample.setOrderByClause("vouchers_createtime DECS");
			break;
		case 2:
			vouchersExample.setOrderByClause("vouchers_createtime");
			break;
		case 3:
			criteria.andVouchersStateEqualTo(1);
			break;
		default:
			break;
		}
		
		Page<Vouchers> page = new Page<Vouchers>();
		page.setData(vouchersMapper.selectByExample(vouchersExample));
		page.setPageCount(pageCount);
		page.setPageSize(pageSize);
		page.setTotalCount(vouchersMapper.countByExample(vouchersExample));
		return page;
	}

	@Override
	public int addVoucher(Vouchers voucher)
	{
		voucher.setVouchersId(null);
		//Calendar calendar = Calendar.getInstance();
		//voucher.setVouchersCreatetime(calendar.getTime());
		//voucher.setVouchersState(1);
		/*calendar.add(Calendar.DATE, voucher.getVouchersDate());
		voucher.setVouchersFailuretime(calendar.getTime());*/
		return vouchersMapper.insertSelective(voucher);
	}

	@Override
	public int deleteVoucher(int voucherId)
	{		
		return vouchersMapper.deleteByPrimaryKey(voucherId);
	}

	@Override
	public int updateVoucher(Vouchers voucher)
	{
		/*Vouchers vouchersOld = vouchersMapper.selectByPrimaryKey(voucher.getVouchersId());
		if(vouchersOld==null)
		{
			return 0;
		}
		voucher.setVouchersCreatetime(null);
		voucher.setVouchersState(null);*/
		/*if(voucher.getVouchersDate()!=null&&voucher.getVouchersDate()!=vouchersOld.getVouchersDate())
		{
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(vouchersOld.getVouchersCreatetime());
			calendar.add(Calendar.DATE, voucher.getVouchersDate());
		}*/
		voucher.setVouchersCreatetime(null);
		voucher.setVouchersState(null);
		voucher.setVouchersType(null);
		return vouchersMapper.updateByPrimaryKeySelective(voucher);
	}

	@Override
	public int closeVoucher(int voucherId)
	{
		Vouchers voucher = vouchersMapper.selectByPrimaryKey(voucherId);
		voucher.setVouchersState(0);
		return vouchersMapper.updateByPrimaryKeySelective(voucher);
	}

	@Override
	public List<Vouchers> getVouchersWithoutPaging(int sequence)
	{
		VouchersExample vouchersExample = new VouchersExample();
		Criteria criteria = vouchersExample.or();
		switch (sequence)
		{

		default:
			break;
		}
		vouchersExample.setOrderByClause("vouchers_createtime DESC");
		return vouchersMapper.selectByExample(vouchersExample);
	}

	@Override
	public Vouchers getVoucherById(int voucherId)
	{
	
		return vouchersMapper.selectByPrimaryKey(voucherId);
	}

	@Override
	public void openVoucher(int voucherId)
	{
		Vouchers vouchers = new Vouchers();
		vouchers.setVouchersId(voucherId);
		vouchers.setVouchersState(1);
		vouchersMapper.updateByPrimaryKeySelective(vouchers);		
	}

}
